package com.syqc.custom

import android.content.Context
import android.os.Build
import android.text.Html
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.LinearLayout
import androidx.core.view.isVisible
import com.syqc.monitor.R
import com.syqc.monitor.databinding.CustomStartEditEndBinding

class StartEndEditText : LinearLayout {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView(context, attrs)
    }

    private lateinit var binding: CustomStartEditEndBinding
    private fun initView(context: Context, attrs: AttributeSet?) {
        binding = CustomStartEditEndBinding.inflate(LayoutInflater.from(context), this, true)
        val typedArray =
            context.obtainStyledAttributes(attrs, R.styleable.editStartEnd)
        val endWeight = typedArray.getFloat(R.styleable.editStartEnd_endWeight, 2f)
        val maxLength = typedArray.getInt(R.styleable.editStartEnd_maxLength, 24)
        val testSize = typedArray.getFloat(R.styleable.editStartEnd_testSize, 14f)
        val endInputType = typedArray.getInt(R.styleable.editStartEnd_endInputType, 0)
        val background = typedArray.getDrawable(R.styleable.editStartEnd_backgroundDrawable)
        val startGravity = typedArray.getInt(R.styleable.editStartEnd_startGravity, 0)
        val endGravity = typedArray.getInt(R.styleable.editStartEnd_endGravity, 0)
        val startText = typedArray.getString(R.styleable.editStartEnd_startText)
        val endText = typedArray.getString(R.styleable.editStartEnd_endText)
        val endHint = typedArray.getString(R.styleable.editStartEnd_endHint)
        val isLineChange = typedArray.getBoolean(R.styleable.editStartEnd_lineChange, false)
        val isLineGone = typedArray.getBoolean(R.styleable.editStartEnd_lineGone, false)
        val isInput = typedArray.getBoolean(R.styleable.editStartEnd_isInput, true)
        when (startGravity) {
            0 -> binding.tvStart.gravity = Gravity.START or Gravity.CENTER_VERTICAL
            1 -> binding.tvStart.gravity = Gravity.CENTER
            2 -> binding.tvStart.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        }
        when (endGravity) {
            0 -> binding.editEnd.gravity = Gravity.START or Gravity.CENTER_VERTICAL
            1 -> binding.editEnd.gravity = Gravity.CENTER
            2 -> binding.editEnd.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        }
        if (background != null)
            this.background = background

        when (endInputType) {
            0 -> binding.editEnd.inputType = InputType.TYPE_CLASS_TEXT
            1 -> binding.editEnd.inputType = InputType.TYPE_CLASS_NUMBER
        }
        binding.tvStart.textSize = testSize
        val params = LayoutParams(0, LayoutParams.MATCH_PARENT)
        params.weight = endWeight
        binding.editEnd.layoutParams = params
        if (isInput) {
            setStartTitle(startText ?: "")
        } else {
            binding.tvStart.text = startText ?: ""
        }
        binding.editEnd.setText(endText ?: "")
        binding.editEnd.hint = endHint ?: ""

        binding.editEnd.filters = arrayOf<InputFilter>(LengthFilter(maxLength))


        if (isLineChange) {
            binding.bottomLine.isVisible = false
            binding.verticalLine.isVisible = false
            binding.bottomLine12.isVisible = true
            binding.verticalLine16.isVisible = true
        }
        if (isLineGone) {
            binding.bottomLine.isVisible = false
        }
        typedArray.recycle()
    }

    private fun setStartTitle(startText: String) {
        val htmlStr = "<font color='#111111'>$startText</font><font color='#FF0000'>*</font>"
        binding.tvStart.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(htmlStr, Html.FROM_HTML_MODE_LEGACY)
        } else {
            Html.fromHtml(htmlStr)
        }
    }

    fun setEndText(endText: String) {
        binding.editEnd.setText(endText)
        binding.editEnd.setSelection(binding.editEnd.text.length)
    }

    fun getEndText(): String {
        return binding.editEnd.text.toString()
    }

    fun getEndEditText(): EditText {
        return binding.editEnd
    }
}